{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Untitled1.ipynb",
      "version": "0.3.2",
      "provenance": [],
      "private_outputs": true,
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/yejianfeng2014/AI/blob/master/Untitled1.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "metadata": {
        "id": "bOcThGLtLj50",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "import tensorflow as tf\n",
        "\n",
        "from  tensorflow.examples.tutorials.mnist import input_data\n",
        "\n",
        "# 1.设置输入和输出节点的个数,配置神经网络的参数。\n",
        "INPUT_NODE = 784  # 输入节点\n",
        "OUTPUT_NODE = 10  # 输出节点\n",
        "LAYER1_NODE = 500  # 隐藏层数\n",
        "\n",
        "BATCH_SIZE = 100  # 每次batch打包的样本个数\n",
        "\n",
        "# 模型相关的参数\n",
        "LEARNING_RATE_BASE = 0.8\n",
        "LEARNING_RATE_DECAY = 0.99\n",
        "REGULARAZTION_RATE = 0.0001\n",
        "TRAINING_STEPS = 5000\n",
        "MOVING_AVERAGE_DECAY = 0.99\n",
        "\n",
        "\n",
        "# 2. 定义辅助函数来计算前向传播结果，使用ReLU做为激活函数。\n",
        "\n",
        "def inference(input_tensor, avg_class, weights1, biases1, weights2, biases2):\n",
        "    # 不使用滑动平均类\n",
        "    if avg_class == None:\n",
        "        layer1 = tf.nn.relu(tf.matmul(input_tensor, weights1) + biases1)\n",
        "        return tf.matmul(layer1, weights2) + biases2\n",
        "\n",
        "    else:\n",
        "        # 使用滑动平均类\n",
        "        layer1 = tf.nn.relu(tf.matmul(input_tensor, avg_class.average(weights1)) + avg_class.average(biases1))\n",
        "        return tf.matmul(layer1, avg_class.average(weights2)) + avg_class.average(biases2)\n",
        "\n",
        "\n",
        "# 3. 定义训练过程。\n",
        "\n",
        "def train(mnist):\n",
        "    x = tf.placeholder(tf.float32, [None, INPUT_NODE], name='x-input')\n",
        "    y_ = tf.placeholder(tf.float32, [None, OUTPUT_NODE], name='y-input')\n",
        "    # 生成隐藏层的参数。\n",
        "    weights1 = tf.Variable(tf.truncated_normal([INPUT_NODE, LAYER1_NODE], stddev=0.1))\n",
        "    biases1 = tf.Variable(tf.constant(0.1, shape=[LAYER1_NODE]))\n",
        "    # 生成输出层的参数。\n",
        "    weights2 = tf.Variable(tf.truncated_normal([LAYER1_NODE, OUTPUT_NODE], stddev=0.1))\n",
        "    biases2 = tf.Variable(tf.constant(0.1, shape=[OUTPUT_NODE]))\n",
        "\n",
        "    # 计算不含滑动平均类的前向传播结果\n",
        "    y = inference(x, None, weights1, biases1, weights2, biases2)\n",
        "\n",
        "    # 定义训练轮数及相关的滑动平均类\n",
        "    global_step = tf.Variable(0, trainable=False)\n",
        "    variable_averages = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step)\n",
        "    variables_averages_op = variable_averages.apply(tf.trainable_variables())\n",
        "    average_y = inference(x, variable_averages, weights1, biases1, weights2, biases2)\n",
        "\n",
        "    # 计算交叉熵及其平均值\n",
        "    cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_, 1))\n",
        "    cross_entropy_mean = tf.reduce_mean(cross_entropy)\n",
        "\n",
        "    # 损失函数的计算\n",
        "    regularizer = tf.contrib.layers.l2_regularizer(REGULARAZTION_RATE)\n",
        "\n",
        "    regularaztion = regularizer(weights1) + regularizer(weights2)\n",
        "\n",
        "    loss = cross_entropy_mean + regularaztion\n",
        "\n",
        "    # 设置指数衰减的学习率。\n",
        "    learning_rate = tf.train.exponential_decay(\n",
        "        LEARNING_RATE_BASE,\n",
        "        global_step,\n",
        "        mnist.train.num_examples / BATCH_SIZE,\n",
        "        LEARNING_RATE_DECAY,\n",
        "        staircase=True)\n",
        "\n",
        "    # 优化损失函数\n",
        "    train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step)\n",
        "\n",
        "    # 反向传播更新参数和更新每一个参数的滑动平均值\n",
        "    with tf.control_dependencies([train_step, variables_averages_op]):\n",
        "        train_op = tf.no_op(name='train')\n",
        "\n",
        "    # 计算正确率\n",
        "    correct_prediction = tf.equal(tf.argmax(average_y, 1), tf.argmax(y_, 1))\n",
        "    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n",
        "\n",
        "    # 初始化会话，并开始训练过程。\n",
        "    with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:\n",
        "        tf.global_variables_initializer().run()\n",
        "        validate_feed = {x: mnist.validation.images, y_: mnist.validation.labels}\n",
        "        test_feed = {x: mnist.test.images, y_: mnist.test.labels}\n",
        "\n",
        "        # 循环的训练神经网络。\n",
        "        for i in range(TRAINING_STEPS):\n",
        "            if i % 1000 == 0:\n",
        "                validate_acc = sess.run(accuracy, feed_dict=validate_feed)\n",
        "                print(\"After %d training step(s), validation accuracy using average model is %g \" % (i, validate_acc))\n",
        "\n",
        "            xs, ys = mnist.train.next_batch(BATCH_SIZE)\n",
        "            sess.run(train_op, feed_dict={x: xs, y_: ys})\n",
        "\n",
        "        test_acc = sess.run(accuracy, feed_dict=test_feed)\n",
        "        print((\"After %d training step(s), test accuracy using average model is %g\" % (TRAINING_STEPS, test_acc)))\n",
        "\n",
        "\n",
        "def main(argv=None):\n",
        "    mnist = input_data.read_data_sets(\"../../../data/MNIST_data\", one_hot=True)\n",
        "    train(mnist)\n",
        "\n",
        "\n",
        "if __name__ == '__main__':\n",
        "    main()\n",
        "\n",
        "\n",
        "# After 5000 training step(s), test accuracy using average model is 0.9836"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "F108o6qXL6-g",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "import os\n",
        "os.environ[\"TF_CPP_MIN_LOG_LEVEL\"]='1' # 这是默认的显示等级，显示所有信息\n",
        "\n",
        "import tensorflow as tf\n",
        "a = tf.constant([1.0,2.0,3.0],shape=[3],name= 'a')\n",
        "b = tf.constant([1.0,2.0, 3.0],shape= [3] ,name = 'b')\n",
        "c = a +b \n",
        "sess = tf.Session(config = tf.ConfigProto(log_device_placement = True))\n",
        "print(sess.run(c))"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "l25wNoagMgxx",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "!cat /proc/meminfo"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "9xsBZsIYRfW1",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "！ls\n"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "ayYtWqGORloj",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "!pwd"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "SYbEeC1CRnN5",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "!ls -a"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "CW13NdIoRpe8",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "!cd config"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "koI5zEhURsO2",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "! cd sample_data"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "ILHS6XStRxpB",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "!ls -a"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "wvnzBiO6Rzq0",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "tf.test.gpu_device_name()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "u78H9ncKR7zm",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        " from tensorflow.python.client import device_lib\n",
        " device_lib.list_local_devices()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "je-T9ezeSILK",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "1 # -*- coding: utf-8 -*-\n",
        " 2 \n",
        " 3 \n",
        " 4 ‘‘‘Trains a simple convnet on the MNIST dataset.\n",
        " 5 Gets to 99.25% test accuracy after 12 epochs\n",
        " 6 (there is still a lot of margin for parameter tuning).\n",
        " 7 16 seconds per epoch on a GRID K520 GPU.\n",
        " 8 ‘‘‘\n",
        " 9 \n",
        "10 from __future__ import print_function\n",
        "11 import keras\n",
        "12 from keras.datasets import mnist\n",
        "13 from keras.models import Sequential\n",
        "14 from keras.layers import Dense, Dropout, Flatten\n",
        "15 from keras.layers import Conv2D, MaxPooling2D\n",
        "16 from keras import backend as K\n",
        "17 \n",
        "18 batch_size = 128\n",
        "19 num_classes = 10\n",
        "20 epochs = 12\n",
        "21 \n",
        "22 # input image dimensions\n",
        "23 img_rows, img_cols = 28, 28\n",
        "24 \n",
        "25 # the data, shuffled and split between train and test sets\n",
        "26 (x_train, y_train), (x_test, y_test) = mnist.load_data()\n",
        "27 \n",
        "28 if K.image_data_format() == ‘channels_first‘:\n",
        "29     x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)\n",
        "30     x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)\n",
        "31     input_shape = (1, img_rows, img_cols)\n",
        "32 else:\n",
        "33     x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)\n",
        "34     x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)\n",
        "35     input_shape = (img_rows, img_cols, 1)\n",
        "36 \n",
        "37 x_train = x_train.astype(‘float32‘)\n",
        "38 x_test = x_test.astype(‘float32‘)\n",
        "39 x_train /= 255\n",
        "40 x_test /= 255\n",
        "41 print(‘x_train shape:‘, x_train.shape)\n",
        "42 print(x_train.shape[0], ‘train samples‘)\n",
        "43 print(x_test.shape[0], ‘test samples‘)\n",
        "44 \n",
        "45 # convert class vectors to binary class matrices\n",
        "46 y_train = keras.utils.to_categorical(y_train, num_classes)\n",
        "47 y_test = keras.utils.to_categorical(y_test, num_classes)\n",
        "48 \n",
        "49 model = Sequential()\n",
        "50 model.add(Conv2D(32, kernel_size=(3, 3),\n",
        "51                  activation=‘relu‘,\n",
        "52                  input_shape=input_shape))\n",
        "53 model.add(Conv2D(64, (3, 3), activation=‘relu‘))\n",
        "54 model.add(MaxPooling2D(pool_size=(2, 2)))\n",
        "55 model.add(Dropout(0.25))\n",
        "56 model.add(Flatten())\n",
        "57 model.add(Dense(128, activation=‘relu‘))\n",
        "58 model.add(Dropout(0.5))\n",
        "59 model.add(Dense(num_classes, activation=‘softmax‘))\n",
        "60 \n",
        "61 model.compile(loss=keras.losses.categorical_crossentropy,\n",
        "62               optimizer=keras.optimizers.Adadelta(),\n",
        "63               metrics=[‘accuracy‘])\n",
        "64 \n",
        "65 model.fit(x_train, y_train,\n",
        "66           batch_size=batch_size,\n",
        "67           epochs=epochs,\n",
        "68           verbose=1,\n",
        "69           validation_data=(x_test, y_test))\n",
        "70 score = model.evaluate(x_test, y_test, verbose=0)\n",
        "71 print(‘Test loss:‘, score[0])\n",
        "72 print(‘Test accuracy:‘, score[1])"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "oTNesur-SjId",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "!pip install -q keras\n",
        "import keras"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "Q03zUwlxS8aH",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "print(keras.__version__)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "-IJt0iCeTKMf",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "print(cv2.__version__)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "BvXf5_VpTT1F",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "!apt-get -qq install -y libsm6 libxext6 && pip install -q -U opencv-python\n",
        "import cv2"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "TKut8-qXTXPH",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "print(cv2.__version__)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "tfmOJ-vLTdAU",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "!pip install nunmpy"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "w1uQu9XJTsIA",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        ""
      ],
      "execution_count": 0,
      "outputs": []
    }
  ]
}